extends Position3D

#export (PackedScene) var object
export var game_item_id = 0

export (bool) var is_active = true

var spawned_object
var spawned_item_object

func _ready():
	
	
	#spawn() #normally it should be commented out, can be used when developing item features, debugging or testing new items
	pass



func spawn():
	if (is_active == true):
		
		var game_item_data = get_node("/root/GameItemList").find_game_item_data_by_id(game_item_id)
		
		print (game_item_data)
		if (game_item_data != null):

			#add the item container first
			var itemContainer = load("res://Scenes/Objects/ItemContainer.tscn")
			spawned_object = itemContainer.instance()
			spawned_object.item_type = game_item_id
			add_child(spawned_object)


			var itemModelSceneName = get_node("/root/GameItemList").get_game_item_value(game_item_data, "itemModelScene")
			
			#check if the item has an item model scene in the items.json file, if not fallback to default
			if itemModelSceneName == null:
				itemModelSceneName = "DefaultItemMesh.tscn"
			print (itemModelSceneName)
			
			#check if the given scene exists in the right directory, if not fallback to default
			if (load("res://Scenes/Objects/ItemModelScenes/" + itemModelSceneName) == null):
				print ("failed to find item model scene: " + str(itemModelSceneName) + ", fallback to default")
				itemModelSceneName = "DefaultItemMesh.tscn"
				
				
			#load item model scene and add it to the item container	
			var itemModelScenePath = "res://Scenes/Objects/ItemModelScenes/" + itemModelSceneName
			var itemModelScene  = load(itemModelScenePath)
			if itemModelScene != null:
				spawned_item_object = itemModelScene.instance()
				spawned_object.add_child(spawned_item_object)


	